package ch08_Swing高级组件.table;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;

//自定义表格单元渲染器
public class StudentTableCellRenderer extends DefaultTableCellRenderer{
	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
												   int row, int column) {
		setHorizontalAlignment(column==3?RIGHT:CENTER);			//专业列居右，其他列居中
		setBackground(new Color(row%2==0?0xFFFFFF:0xE4E4E4));	//隔行换色
		if(column==4) {											//是否为党员列以复选按钮显示
			JPanel cell = new JPanel(new BorderLayout());		//构造面板组件
			JCheckBox cb = new JCheckBox("",(Boolean)value);	//根据单元格值构造复选按钮组件
			cb.setHorizontalAlignment(CENTER);					//复选按钮居中
			if(isSelected) {						//根据当前行是否被选中，设置复选按钮背景色
				cb.setBackground(new Color(184,207,229));//184,207,229
			}else {
				cb.setBackground(new Color(row%2==0?0xFFFFFF:0xE4E4E4));
			}
			cell.add(cb);				//加入面板
			return cell;				//返回面板组件
		}
		//其他列调用父类方法做默认渲染（默认为标签）
		return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
	}



}
